<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MX_Controller {
  function __construct() {
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->model('users/musers');
    $this->load->helper('string');
    $this->menu = $this->cek();
  }
	public function add_new($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Users Edit");
    $data_privilege = 0;
    $data_user_privilege = 0;
    if($id > 0){
      $data_detail = $this->musers->get_detail($id);
      $data_privilege = $this->global_models->get_field("d_user_privilege", "id_privilege", array("id_users" => $id));
      $data_user_privilege = $this->global_models->get_field("d_user_privilege", "id_user_privilege", array("id_users" => $id));
//      $this->debug($data_privilege, true);
    }
    else{
      $data_detail->name = "";
      $data_detail->email = "";
      $data_detail->id_status_user = "";
      $data_detail->id_users = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $dropdown = $this->global_models->get_dropdown("m_privilege", "id_privilege", "name", TRUE, array("parent > " => 0));
      $this->template->build('add-new', 
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'users',
              'title'   => 'Add User',
              'detail'  => array($data_detail),
              'dropdown'  => array($dropdown),
              'selectd'  => array($data_privilege),
              'id_relasi'  => array($data_user_privilege),
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
            'name'            =>  $this->input->post("name", TRUE),
            'email'           =>  $this->input->post("email", TRUE),
            'id_status_user'  =>  $this->input->post("status", TRUE)
        );
        if($this->input->post("pass", TRUE)){
          if($this->input->post("pass", TRUE) == $this->input->post("repass", TRUE)){
            $kirim['pass'] = $this->encrypt->encode($this->input->post("pass", TRUE));
          }
        }
        $hasil_in = $this->musers->update_user($this->input->post("id_detail", TRUE), $kirim);
        if($hasil_in AND $this->input->post("privilege", TRUE) != 0){
          $hasil_pr = $this->global_models->update_duplicate("d_user_privilege", 
                  array(
                      "id_user_privilege" => $this->input->post("id_relasi", TRUE),
                      "id_privilege"  =>  $this->input->post("privilege", TRUE),
                      "id_users"  =>  $this->input->post("id_detail", TRUE),
                      "update_by_users" => $this->session->userdata('id')
                      ), 
                  array("id_privilege" => $this->input->post("privilege", TRUE)));
          redirect ("users/index/list/sukses");
        }
        else
          redirect ("users/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
//        if(is_array($pst['name'])){
//          foreach($pst['name'] as $key => $pst_name){
        if($pst['pass'] == $pst['repass']){
          $kirim = array(
              'name'            =>  $pst['name'],
              'pass'            =>  $this->encrypt->encode($pst['pass']),
              'email'           =>  $pst['email'],
              'id_status_user'  =>  $pst['status'],
              "create_by_users" => $this->session->userdata('id'),
              "create_date" =>  date("Y-m-d H:i:s"),
              "update_by_users" => $this->session->userdata('id')
          );
          $hasil_in = $this->musers->insert_user($kirim);
          if($hasil_in){
            if($pst['privilege'] > 0){
              $kirim_privilege = array(
                  'id_privilege'    =>  $pst['privilege'],
                  'id_users'        =>  $hasil_in,
              );
              $hasil_pr = $this->global_models->insert("d_user_privilege", $kirim_privilege);
            }
          }
         }
         redirect ("users/index/list/sukses");
//          }
//          if(is_array($res))
//            redirect ("users/index/list/sukses");
//          else
//            redirect ("users/index/list/filed");
//        }
      }
    }
  }
	public function delete($id){
    if($this->musers->change_status($id, 3))
      redirect("users/index/list/sukses");
    else
      redirect("users/index/list/filed");
  }
	public function status($id, $status){
    if($this->musers->change_status($id, $status))
      redirect("users/index/list/sukses");
    else
      redirect("users/index/list/filed");
  }
	public function export_xls(){
    $this->musers->export_xls("data-users");
  }
	public function index($action = "list", $pesan = "hal", $hal = 0){
    $list = $this->musers->get();
//    $config['base_url'] = site_url("users/index/".$action."/".$pesan."/");
//    $config['uri_segment'] = 5;
//    $config['num_links'] = 3;
//    $config['total_rows'] = $list['total'];
//    $config['per_page'] = 10; 

//    $this->pagination->initialize($config); 
//    $this->debug($list, true);
    $this->template->title('Sistem', "Users");
    
    $this->template->build('main', 
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'users',
            'data'    => $list,
          ));
    $this->template
      ->set_layout('default')
      ->build('main');
	}
  public function edit_profile($pesan = "hal"){
    $id = $this->session->userdata("id");
    $this->template->title('Sistem', "Users Edit");
    $data_privilege = 0;
    $data_user_privilege = 0;
    if($id > 0){
      $data_detail = $this->musers->get_detail($id);
      $data_privilege = $this->global_models->get_field("d_user_privilege", "id_privilege", array("id_users" => $id));
      $data_user_privilege = $this->global_models->get_field("d_user_privilege", "id_user_privilege", array("id_users" => $id));
//      $this->debug($data_privilege, true);
    }
    else{
      $data_detail->name = "";
      $data_detail->email = "";
      $data_detail->id_status_user = "";
      $data_detail->id_users = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $dropdown = $this->global_models->get_dropdown("m_privilege", "id_privilege", "name");
      $this->template->build('edit-profile', 
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'users',
              'title'   => 'Add User',
              'detail'  => array($data_detail),
              'dropdown'  => array($dropdown),
              'selectd'  => array($data_privilege),
              'id_relasi'  => array($data_user_privilege),
            ));
      $this->template
        ->set_layout('default')
        ->build('edit-profile');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
            'email'           =>  $this->input->post("email", TRUE)
        );
        if($this->input->post("pass", TRUE)){
          if($this->input->post("pass", TRUE) == $this->input->post("repass", TRUE)){
            $kirim['pass'] = $this->encrypt->encode($this->input->post("pass", TRUE));
          }
        }
        $hasil_in = $this->musers->update_user($this->input->post("id_detail", TRUE), $kirim);
        if($hasil_in){
          redirect ($this->session->userdata('dashbord'));
        }
        else
          redirect ($this->session->userdata('dashbord'));
      }
    }
  }
  
  public function generate_password($id_users) {
      $new_password = random_string('alnum',8);
      $enpass = $this->encrypt->encode($new_password);
      $email_user = $this->global_models->get("m_users", array("id_users" => $id_users));
      if($id_users > 0) {
        $this->global_models->update('m_users', array("id_users" => $id_users),array('pass' => $enpass));
                
        //kirim email
        $this->email->from('no-reply@technomotor.co.id', 'Aplikasi Mr.Montir');
        $this->email->to($email_user[0]->email);

        $this->email->subject('Notifikasi Perubahan Password');
        $this->email->message("
          Berikut adalah akunt untuk akses Outlet Monitoring :
          link => ".base_url()."
          user => {$email_user[0]->name}
          pass => {$new_password}
          ");

        if($this->email->send() === TRUE){
          redirect ("users");
        }
        else
          redirect ("home");
      }
  }
  public function email_pass($id_users) {
      $email_user = $this->global_models->get("m_users", array("id_users" => $id_users));
      if($id_users > 0) {
                
        //kirim email
        $this->email->from('no-reply@technomotor.co.id', 'Aplikasi Mr.Montir');
        $this->email->to($email_user[0]->email);

        $this->email->subject('Notifikasi Perubahan Password');
        $this->email->message("
          Berikut adalah akunt untuk akses Outlet Monitoring :
          link => ".base_url()."
          user => {$email_user[0]->name}
          pass => ".$this->encrypt->decode($email_user[0]->pass)."
          ");

        if($this->email->send() === TRUE){
          redirect ("users");
        }
        else
          redirect ("home");
      }
  }
  public function generate_password_no_mail($id_users) {
      $new_password = random_string('alnum',8);
      $enpass = $this->encrypt->encode($new_password);
      $email_user = $this->global_models->get("m_users", array("id_users" => $id_users));
      if($id_users > 0) {
        $this->global_models->update('m_users', array("id_users" => $id_users),array('pass' => $enpass));
        
        redirect ("outlet/master-outlet");
        
      }
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */